class Topic < ActiveRecord::Base
  belongs_to :user
  has_many :articles
  has_many :tag_topic_ships
  has_many :tags, through: :tag_topic_ships

  has_many :follow_topic_ships
  has_many :followers,
           through: :follow_topic_ships,
           source: :user

  default_scope { order('created_at DESC') }
  default_scope { where(is_deleted: false) }

  validates :title,
            presence: {:message => '标题不能为空'},
            :length => {:minimum => 4, :maximum => 50, :message => "4～50字"}

  validates :intro,
            presence: false,
            :length => {:maximum => 500, :message => "最多500个字"}

  validates :tags,
            presence: {:message => '请输入标签'},
            :length => {:minimum => 1, :maximum => 5, :message => "标签数量错误，最多5个标签"}

  validates :score,
            presence: {:message => '请输入所需积分'},
            :length => {:minimum => 0, :maximum => 100, :message => "积分范围: 0~100分"}

  #
  # searchable do
  #   text :title
  #   text :intro
  # end

end
